package cn.wolfcdoe.web.controller.audit;

import cn.wolfcdoe.audit.domain.BusBpmnInfo;
import cn.wolfcdoe.audit.service.IBusBpmnInfoService;
import cn.wolfcdoe.common.annotation.Log;
import cn.wolfcdoe.common.core.controller.BaseController;
import cn.wolfcdoe.common.core.domain.AjaxResult;
import cn.wolfcdoe.common.core.page.TableDataInfo;
import cn.wolfcdoe.common.enums.BusinessType;
import cn.wolfcdoe.common.utils.poi.ExcelUtil;
import org.activiti.engine.RepositoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

/**
 * 流程定义Controller
 * 
 * @author zhuo
 * @date 2023-04-16
 */
@RestController
@RequestMapping("/audit/bpmnInfo")
public class BusBpmnInfoController extends BaseController
{
    @Autowired
    private IBusBpmnInfoService busBpmnInfoService;

    /**
     * 查询流程定义列表
     */
    @PreAuthorize("@ss.hasPermi('audit:bpmInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusBpmnInfo busBpmnInfo)
    {
        startPage();
        List<BusBpmnInfo> list = busBpmnInfoService.selectBusBpmnInfoList(busBpmnInfo);
        return getDataTable(list);
    }

    /**
     * 导出流程定义列表
     */
    @PreAuthorize("@ss.hasPermi('audit:bpmInfo:export')")
    @Log(title = "流程定义", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusBpmnInfo busBpmnInfo)
    {
        List<BusBpmnInfo> list = busBpmnInfoService.selectBusBpmnInfoList(busBpmnInfo);
        ExcelUtil<BusBpmnInfo> util = new ExcelUtil<BusBpmnInfo>(BusBpmnInfo.class);
        util.exportExcel(response, list, "流程定义数据");
    }

    /**
     * 获取流程定义详细信息
     */
    @PreAuthorize("@ss.hasPermi('audit:bpmInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(busBpmnInfoService.selectBusBpmnInfoById(id));
    }

    /**
     * 新增流程定义
     */
    @PreAuthorize("@ss.hasPermi('audit:bpmInfo:add')")
    @Log(title = "流程定义", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(BusBpmnInfo busBpmnInfo, MultipartFile file) throws IOException {
        return toAjax(busBpmnInfoService.saveBusBpmn(busBpmnInfo,file.getInputStream()));
    }

    /**
     * 修改流程定义
     */
    @PreAuthorize("@ss.hasPermi('audit:bpmInfo:edit')")
    @Log(title = "流程定义", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusBpmnInfo busBpmnInfo)
    {
        return toAjax(busBpmnInfoService.updateBusBpmnInfo(busBpmnInfo));
    }

    /**
     * 删除流程定义
     */
    @PreAuthorize("@ss.hasPermi('audit:bpmInfo:remove')")
    @Log(title = "流程定义", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(busBpmnInfoService.deleteBusBpmnInfoById(id));
    }


    /**
     * 查看流程定义文件和图,响应文件内容,一般用来响应文件内容
     * 根据需求修来响应内容,响应文件内容,文件下载
     * 接口方法写 void,由开发来自定义响应内容
     */
    @GetMapping("/queryProccess/{id}/{type}")
    public void queryProcess(@PathVariable Long id , @PathVariable String type,HttpServletResponse response) throws IOException {
        InputStream in = busBpmnInfoService.getInputStream(id,type);
        //把文件内容封装成一个InputStream对象
        FileCopyUtils.copy(in,response.getOutputStream());
    }

}
